var net = require('net');
var server = net.createServer();

var clientList = [];
server.on('connection', function(client) {

	clientConnected(client);

});


server.listen(8080,function(err){
        if(err){
            console.error('bind err');
        }else{
            console.log('bind 8080 success');
        }
    });
    
    
    
var clientConnected = function(client){
	client.name = client.remoteAddress + ':' + client.remotePort;
	console.log('client connected:'+client.name);
	clientList.push(client);
	client.on('data', function(data) {
		console.log('client:'+client.name+' data:');
		console.log(data);
		clientRecvData(client,data);
	});
	client.on('end', function() {
		console.log('client disconnected :'+client.name);
		clientList.splice(clientList.indexOf(client), 1)
	});
	client.on('error', function(e) {
		console.log('client error:'+client.name);
		console.log(e);
	});
};

var clientRecvData = function(client,data){
    client.write('server has recvie u data, and send it to u:');
    client.write(data);
}
